package com.macro.mall.service.impl;

import com.macro.mall.mapper.BmsTourismProjectActivitiesPicMapper;
import com.macro.mall.model.BmsTourismProjectActivitiesPic;
import com.macro.mall.model.BmsTourismProjectActivitiesPicExample;
import com.macro.mall.service.BmsTourismProjectActivitiesPicService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BmsTourismProjectActivitiesPicServiceImpl implements BmsTourismProjectActivitiesPicService {

    @Autowired
    private BmsTourismProjectActivitiesPicMapper bmsTourismProjectActivitiesPicMapper;

    @Override
    public void create(Long tourismProjectActivitiesId, String coverPic, List<String> picUrlList) {

        BmsTourismProjectActivitiesPic itemObject;

        if (StringUtils.isNotBlank(coverPic)) {
            itemObject = new BmsTourismProjectActivitiesPic();
            itemObject.setPic(coverPic);
            itemObject.setTourismProjectActivitiesId(tourismProjectActivitiesId);
            itemObject.setIsCover((byte) 1);
            bmsTourismProjectActivitiesPicMapper.insert(itemObject);
        }

        if (CollectionUtils.isNotEmpty(picUrlList)) {
            for (String pic : picUrlList) {
                itemObject = new BmsTourismProjectActivitiesPic();
                itemObject.setPic(pic);
                itemObject.setTourismProjectActivitiesId(tourismProjectActivitiesId);
                itemObject.setIsCover((byte) 0);
                bmsTourismProjectActivitiesPicMapper.insert(itemObject);
            }
        }
    }

    @Override
    public void deleteByActivitiesId(Long tourismProjectActivitiesId) {
        BmsTourismProjectActivitiesPicExample example = new BmsTourismProjectActivitiesPicExample();
        BmsTourismProjectActivitiesPicExample.Criteria criteria = example.createCriteria();
        criteria.andTourismProjectActivitiesIdEqualTo(tourismProjectActivitiesId);
        bmsTourismProjectActivitiesPicMapper.deleteByExample(example);
    }

    @Override
    public String findCoverPicByActivitiesId(Long tourismProjectActivitiesId) {
        BmsTourismProjectActivitiesPicExample example = new BmsTourismProjectActivitiesPicExample();
        BmsTourismProjectActivitiesPicExample.Criteria criteria = example.createCriteria();
        criteria.andTourismProjectActivitiesIdEqualTo(tourismProjectActivitiesId);
        criteria.andIsCoverEqualTo((byte) 1);
        List<BmsTourismProjectActivitiesPic> bmsTourismProjectActivitiesPics = bmsTourismProjectActivitiesPicMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(bmsTourismProjectActivitiesPics)) {
            return bmsTourismProjectActivitiesPics.get(0).getPic();
        }
        return null;
    }

    @Override
    public List<String> findPicListByActivitiesId(Long tourismProjectActivitiesId) {
        BmsTourismProjectActivitiesPicExample example = new BmsTourismProjectActivitiesPicExample();
        BmsTourismProjectActivitiesPicExample.Criteria criteria = example.createCriteria();
        criteria.andTourismProjectActivitiesIdEqualTo(tourismProjectActivitiesId);
        criteria.andIsCoverEqualTo((byte) 0);
        List<BmsTourismProjectActivitiesPic> bmsTourismProjectActivitiesPics = bmsTourismProjectActivitiesPicMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(bmsTourismProjectActivitiesPics)) {
            return bmsTourismProjectActivitiesPics.stream().map(BmsTourismProjectActivitiesPic::getPic).collect(Collectors.toList());
        }
        return null;
    }
}
